﻿using AuthCenter.API.Application.Command.AccountRole;
using AuthCenter.API.Application.Command.Role;
using AuthCenter.Domain.Account;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.AccountRole
{
    public class DelRoleUserHandler : IRequestHandler<DelRoleUserCommand, APIResult<int>>
    {
        private readonly IBaseRepository<AccountRoleModel> accounroleRepo;

        public DelRoleUserHandler(IBaseRepository<AccountRoleModel> accounroleRepo)
        {
            this.accounroleRepo = accounroleRepo;
        }
        public Task<APIResult<int>> Handle(DelRoleUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = accounroleRepo.GetAll().FirstOrDefault(x=>x.AccountId == request.AccountId);
            if (res == null)
            {
                result.Message = "找不到此编号";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            
            int r = accounroleRepo.DeleteAsync(res).Result;
            if (r > 0)
            {
                result.Message = "删除员工成功";
                result.Code = APIEnum.Ok;
                result.Data = r;
            }
            else
            {
                result.Message = "删除员工失败";
                result.Code = APIEnum.Fail;
                result.Data = r;
            }
            return Task.FromResult(result);
        }
    }
}
